<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@include file="/WEB-INF/ibnms/common/easyui.jsp" %>
<%@include file="/WEB-INF/ibnms/common/ztree.jsp" %>
<html style="height:100%;overflow:hidden;">
	<head>
		<jsp:include page="/WEB-INF/ibnms/common/configinclude.jsp"></jsp:include>
		<title>
		</title>
	</head>
	<script type="text/javascript">
		var $layout = $("#layout");
		var $zTreeObj;
		var $moduleTreeObj;
		var _roleModuleIds;
		$(document).ready(function(){
			$zTreeObj = $.fn.zTree.init($("#roleManageTree"), 
			{	
				treeId:"id",
				data: {
					key:{
                           name:'name',
                           title:'name',
                           id:'id'
                       },
                       simpleData:{
                           idKey:'id'
                       }				
				},
				async:{
					enable:true,
					type:"post",
					dataType:"json",
					autoParam:["id=node.id"],
					url:"loadRoleModuleTreeByParentId.do"
				},
				view: {
					fontCss: function (treeId, treeNode) {
						return (!!treeNode.highlight) ? {color:"#A60000", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"};
					},
					dblClickExpand:true
				},
				callback:{
					onClick:function(event, treeId, treeNode, clickFlag){
						$moduleTreeObj.checkAllNodes(false);
						$moduleTreeObj.expandAll(false);
						$("#moduleManageTree").hide();
						$("#loadingmoduleTree").show();
						$.ajax({
							type:"post",
							url:"findModuleIdByRoleId.do?roleForm.ROLE_ID="+$(treeNode).attr("id"),
							dataType:"json",
							success:function(msg){
								msg = eval(msg);
								_roleModuleIds = msg;
								checkNode(msg);
							}
						});
						$layout.layout("resize");
					},
					onAsyncSuccess:function(event, treeId, treeNode, msg){
						/*if(msg[0].pId==null||msg[0].pId=='null'){
							var firstNode = $zTreeObj.getNodeByParam('id',msg[0].id,null);
							$(firstNode).click();
							$zTreeObj.selectNode(firstNode,false);
							$("iframe[name='roleDisplayFrame']").attr("src","loadListRole.do?roleForm.PARENT_ID="+msg[0].id);
						}*/
					}
				}
			}, 
			 [
				{"name":"角色管理", id:"null" ,pId:"null"}
			]);
			$moduleTreeObj = $.fn.zTree.init($("#moduleManageTree"), 
			{
				treeId:"id",
				data: {
					key:{
                           name:'name',
                           title:'name',
                           id:'id'
                       },
                       simpleData:{
                           idKey:'id'
                       }				
				},
				check:{
					enable:true,
					autoCheckTrigger:true,
					chkboxType:{ "Y": "sp", "N": "sp" }
				},
				callback:{
					onNodeCreated:function(event, treeId, treeNode){
						if($(treeNode).attr("pId") == "0"){
							$.ajax({
								type:"post",
								url:"loadModuleInfoBySystemId.do?systemId="+$(treeNode).attr("id"),
								dataType:"json",
								success:function(msg){
									msg = eval(msg);
									var newNode = $moduleTreeObj.transformTozTreeNodes(msg);
									$moduleTreeObj.addNodes(treeNode,newNode,true);
									checkNode(_roleModuleIds);
								}
							});
						}
					}
				}
			},[]);
			
			$.ajax({
				type:"post",
				url:"loadRoleModuleTreeByParentId.do",
				dataType:"json",
				success:function(msg){
					var rootNodeObj = $zTreeObj.getNodeByParam("id","null",null);
					$zTreeObj.addNodes(rootNodeObj,msg,false);
				}
			});
			
			$.ajax({
				type:"post",
				url:"loadSystemNodeForJson.do?systemId=0",
				dataType:"json",
				success:function(msg){
					msg = eval(msg);
					$moduleTreeObj.addNodes(null,msg,false);
				}
			});
			
			$("#submitBtn").click(function(){
				var selectNode = $zTreeObj.getSelectedNodes();
				if(selectNode == null || selectNode == ''){
					$.messager.alert('角色选择错误','请选择角色！');
					return;
				}
				var roleId = $(selectNode[0]).attr("id");
				if(roleId == 'null'){
					$.messager.alert('角色选择错误','请选择具体的角色!');
					return;
				}
				// 获取所有被选中的模块节点
				var checkNodes = $moduleTreeObj.getCheckedNodes(true);
				var cfgNodeIds = "";
				$.each(checkNodes,function(idx,node){
					cfgNodeIds += "#" + $(node).attr("id");
				})
				cfgNodeIds = cfgNodeIds.substring(1);
				$.ajax({
					type:"post",
					dataType:"json",
					url:"saveRoleModel.do",
					data:"theForm.roleID="+roleId+"&theForm.moduleIds="+cfgNodeIds,
					beforeSend:function(){
						$("#submitBtn").val("保存中....");
						$("#submitBtn").attr("disabled","disabled");
					},
					success:function(msg){
						if(msg == 'true'){
							$("#submitBtn").val("保存");
							$("#submitBtn").removeAttr("disabled");
							alert("关联成功");
						}
					}
				})
				
			});
			
		});
		
		function checkNode(rel){
			var nodes = $moduleTreeObj.getNodes();
			/*判断哪些节点需要被选中*/
			for(var i=0;i<nodes.length;i++){
				eachChildren(nodes[i],rel);
			}
			$("#loadingmoduleTree").hide();
			$("#moduleManageTree").show();
		}
		
		function eachChildren(node,rel){
			if(node.isParent){
				var childNode = node.children;
				if(childNode != null && node.children.length>0){
					var b = true;
					for(var j = 0 ; j < childNode.length ; j ++){
						eachChildren(childNode[j],rel);
					}
				}else{
					if(rel!=undefined){
						for(var i = 0 ; i < rel.length; i++){
							if(node.id == rel[i]){
								$moduleTreeObj.checkNode(node, true, true);
							}
						}
					}
				}
			}else{
				if(rel!=undefined){
					for(var i = 0 ; i < rel.length; i++){
						if(node.id == rel[i]){
							$moduleTreeObj.checkNode(node, true, true);
						}
					}
				}
			}
		}
		//alert('');
	</script>
<body style="height:100%;">
   	<div class="form-title">
   		 <span class="ttcorner"></span>
         <h3>
            <span class="text">角色信息配置</span>
         </h3>
    </div>
	<div class="easyui-layout" id="layout" style="width:100%;height:96%">
		<div data-options="region:'west',fit:false,split:false,minWidth:200" style="width:50%">
			<ul id="roleManageTree" class="ztree" style="width:95%; height:98%; overflow:auto;"></ul>
		</div>
		<div data-options="region:'center',title:''" style="width:95%;border-right: 0px">
			<div class="easyui-layout" id="layout" style="width:100%" >
				<div data-options="region:'south'" style="width:100%;height:30px;overflow:hidden;padding-left:10px;padding-top:3px;">
					<input id="submitBtn" type="button" class="btn-style02" align="middle" value="保存" />
				</div>
				<div data-options="region:'center',title:''" style="width:95%;border-right: 0px;overflow-x:hidden;overflow-y:hidden">
					<div id='loadingmoduleTree' style='display:none'><img src="<%=application.getContextPath()%>/njs/basd/ztree/css/zTreeStyle/img/loading.gif"></img>load....</div>
					<ul id="moduleManageTree" class="ztree" style="width:100%; height:100%; overflow:auto;"></ul>
				</div>
			</div>
		</div>
	</div>
</body>
</html>
